package jmine.tec.web.front.dao;

import java.util.List;

import jmine.tec.web.front.domain.BackOfficeRequest;
import jmine.tec.web.front.domain.BackOfficeRequestRevision;
import jmine.tec.web.front.domain.BackOfficeRequestRevisionOutcome;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BeanNotFoundException;

/**
 * DAO for BackOfficeRequestRevision
 * 
 * @author lundberg
 */
public class BackOfficeRequestRevisionDAO extends BaseDAO<BackOfficeRequestRevision> {

    /**
     * Finds all revisions for the given property and outcome
     * 
     * @param request request
     * @param outcome outcome
     * @return List of BackOfficeRequestRevision
     */
    public List<BackOfficeRequestRevision> findByRequestOutcome(BackOfficeRequest request, BackOfficeRequestRevisionOutcome outcome) {
        Criteria criteria = this.createCriteria();
        criteria.add(Restrictions.eq("request", request));
        criteria.add(Restrictions.eq("outcome", outcome));
        return this.executeQuery(criteria);
    }

    /**
     * Finds the last revision for the given property and outcome
     * 
     * @param request request
     * @return List of BackOfficeRequestRevision
     * @throws BeanNotFoundException if there is no registry on database.
     */
    public BackOfficeRequestRevision findLastByRequest(BackOfficeRequest request) throws BeanNotFoundException {
        Criteria criteria = this.createCriteria();
        criteria.add(Restrictions.eq("request", request));
        criteria.addOrder(Order.desc("id"));
        criteria.setMaxResults(1);
        return this.executeSingleQuery(criteria);
    }

    /**
     * Find every revision for the given request
     * 
     * @param request request
     * @return list of revisions for the reques
     */
    public List<BackOfficeRequestRevision> findAllByRequest(BackOfficeRequest request) {
        Criteria criteria = this.createCriteria();
        criteria.add(Restrictions.eq("request", request));
        return this.executeQuery(criteria);
    }

}
